Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
What's the job of "return" right underneath the if clause in this exact function? also pls explain in words how this function works ty

Write a function that returns the smallest number that’s greater or equal to 0. Especially that return right underneath if (num < 0). What's that return's job here?

function minNonNegative(numArray) {
  let min = Infinity;

  numArray.forEach(function(num) {
    if (num < 0) {
      return;
    } else if (num < min) {
      min = num
    }
  });
  return min;
}

console.log(minNonNegative([-3,2,5,-9]))

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The return INSIDE the forEach is to ignore the else which is then not even needed:

numArray.forEach(num => { if (num < 0) return; if (num < min) { min = num } });

You can use a filter and Math.min:

const minNonNegative = numArray => Math.min(...numArray.filter(num => num>=0))


console.log(minNonNegative([-3,2,5,-9]))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!